home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / convert / totiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-04  |  5.4 KB  |  194 lines

  1. /*
  2. %    TOTIFF . C
  3. %
  4. % AUTHOR:    Jin Guojun - LBL    1991
  5. */
  6.  
  7. #include "header.def"
  8. #include "imagedef.h"
  9.  
  10. arg_fmt_list_string    arg_fmt[] =    {
  11.     {"-none", "%Nh", COMPRESSION_NONE, 1, 0,
  12.         "no compression at all (default = LZW)"},
  13.     {"-pac", "%Nh", COMPRESSION_PACKBITS, 1, 0, "packbits"},
  14.     {"-g3", "%Nh", COMPRESSION_CCITTFAX3, 1, 0, "compress"},
  15.     {"-g4", "%Nh", COMPRESSION_CCITTFAX4, 1, 0, "compress"},
  16.     {"-pred", "%h", 0, 1, 1, "predictor"},
  17.     {"-row", "%d", 0, 1, 1, "rowsperstrip"},
  18.     {"-msb", "%Nh", FILLORDER_MSB2LSB, 1, 0, "MSB to LSB"},
  19.     {"-lsb", "%Nh", FILLORDER_LSB2MSB, 1, 0, "LSB to MSB"},
  20.     {"-2d", "%|h", GROUP3OPT_2DENCODING, 1, 0, "2D encoding"},
  21.     {"-fill", "%|h", GROUP3OPT_FILLBITS, 1, 0, "fill bits"},
  22.     {"-k", "%b", True, 1, 0, "keep working, ignore input error"},
  23.     {"-revs", "%+", No, 1, 0, "RGB to BGR"},
  24.     {"-sep", "%b", PLANARCONFIG_SEPARATE, 1, 0,
  25.         "SEPARATE PLANE for 24-bit image?"},
  26. {"I/O:    [<] input [[> |] output]", "0", 0, 0, 0, "end of usage"},
  27.     NULL    };
  28.  
  29. bool    revs;
  30. U_IMAGE    uimg;
  31. TIFF*    TIFFout;
  32.  
  33. #define    rows    uimg.height
  34. #define    cols    uimg.width
  35. #define    ocfm    uimg.color_form
  36. #define    SetTiFF(ctrl, val)    TIFFSetField(TIFFout, ctrl, val)
  37. #define    WriteTiFF(buf, w, rgb)    TIFFWriteScanline(TIFFout, buf, w, rgb)
  38.  
  39. main(ac, av)
  40. int    ac;
  41. char*    av[];
  42. {
  43. char*    *fl;
  44. unsigned short    compression=COMPRESSION_LZW, config=PLANARCONFIG_CONTIG,
  45.     fillorder=0, predictor=0, photometric=PHOTOMETRIC_MINISBLACK,
  46.     samplesperpixel=1, bitspersample=8;
  47. long    g3options=0, rowsperstrip;
  48. int    bytesperrow, i, nostop, row;
  49. register byte    *obp;
  50.  
  51.     if ((i=parse_argus(&fl, ac, av, arg_fmt,
  52.         &compression, &compression, &compression, &compression,
  53.         &predictor, &rowsperstrip,
  54.         &fillorder, &fillorder,
  55.         &g3options, &g3options,
  56.         &nostop, &revs,    &config)) < 0)
  57.         exit(i);
  58.     if (predictor < 0 && predictor > 2 || rowsperstrip < 0)    {
  59. info:        parse_usage(arg_fmt);    exit(0);    }
  60.  
  61.     if (i && (in_fp=freopen(fl[0], "rb", stdin)) == NULL)
  62.         syserr("open input %s", fl[0]);
  63.  
  64. uimg.color_dpy = -1;
  65. format_init(&uimg, IMAGE_INIT_TYPE, RLE, TiFF, *av, "S1-2");
  66.  
  67. io_test(fileno(in_fp), goto    info);
  68.  
  69. if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0, Yes))
  70.     syserr("unknown image type");
  71.  
  72. TIFFout = TIFFFdOpen(stdout_fd, "Standard Output", "w");
  73. if (!TIFFout)
  74.     syserr("open TIFF output file");
  75. if (fillorder)
  76.     revs = !revs,
  77.     SetTiFF(TIFFTAG_FILLORDER, fillorder);    /* for tiff.3.0    */
  78. if (revs && ocfm==CFM_ILC)    ocfm = CFM_ILL;
  79. (*uimg.std_swif)(FI_LOAD_FILE, &uimg, nostop ? NULL : uimg.name,
  80.     True /* don't change format, and save PNM loading buffer */);
  81.  
  82.     switch (ocfm) {    /* Figure out TIFF parameters. */
  83.     case CFM_BITMAP:
  84.         bitspersample = 1;
  85.         bytesperrow = (cols + 7) / 8;
  86.         if (uimg.in_color != ocfm)
  87.             uimg.dest = nzalloc(bytesperrow, 1, "bm-obuf");
  88.         if (uimg.in_type==RAS)    photometric = PHOTOMETRIC_MINISWHITE;
  89.         break;
  90.     case CFM_SGF:
  91.         bitspersample = min_bits(255);
  92.         i = 8 / bitspersample;
  93.         bytesperrow = (cols + i - 1) / i;
  94.         break;
  95.     case CFM_SCF:
  96.         photometric = PHOTOMETRIC_PALETTE;
  97.         bytesperrow = cols;
  98.         if (!r_cmap)
  99.             r_cmap = (sht_cmap_t*) rle_dflt_hdr.cmap;
  100.         break;
  101.     case CFM_ILL:
  102.         uimg.dest = nzalloc(cols, 3, "24-obuf");
  103.     case CFM_ILC:
  104.     default:
  105.         samplesperpixel = 3;
  106.         photometric = PHOTOMETRIC_RGB;
  107.         bytesperrow = cols * 3;
  108.     }
  109.  
  110.     if (!rowsperstrip)    rowsperstrip = 8192 / bytesperrow;
  111.  
  112. /* Set TIFF parameters. */
  113.     SetTiFF(TIFFTAG_IMAGEWIDTH, cols);
  114.     SetTiFF(TIFFTAG_IMAGELENGTH, rows);
  115.     SetTiFF(TIFFTAG_BITSPERSAMPLE, bitspersample);
  116.     SetTiFF(TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  117.     SetTiFF(TIFFTAG_COMPRESSION, compression);
  118.     if (compression == COMPRESSION_CCITTFAX3 && g3options)
  119.         SetTiFF(TIFFTAG_GROUP3OPTIONS, g3options);
  120.     if (compression == COMPRESSION_LZW && predictor)
  121.         SetTiFF(TIFFTAG_PREDICTOR, predictor);
  122.     SetTiFF(TIFFTAG_PHOTOMETRIC, photometric);
  123.     if (uimg.desc)
  124.         SetTiFF(TIFFTAG_DOCUMENTNAME, uimg.desc);
  125.     SetTiFF(TIFFTAG_IMAGEDESCRIPTION, "totiff");
  126.     SetTiFF(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  127.     SetTiFF(TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  128.     /*    SetTiFF(TIFFTAG_STRIPBYTECOUNTS, rows / rowsperstrip);    */
  129.     SetTiFF(TIFFTAG_PLANARCONFIG, config);
  130.     if (reg_cmap[0])
  131.         regmap_to_rlemap(reg_cmap, uimg.cmaplen, 3, &rle_dflt_hdr),
  132.         i = 1 << rle_dflt_hdr.cmaplen,
  133.         TIFFSetField(TIFFout, TIFFTAG_COLORMAP,
  134.             r_cmap, r_cmap+i, r_cmap+(i<<1));
  135.  
  136.     obp = (byte*)uimg.src;
  137.  
  138.     /*    Write the TIFF buf    */
  139.     for (row=0; row < rows; row++, obp+=cols) {
  140.     switch (ocfm) {
  141.     case CFM_ILL:
  142.         if    (config == PLANARCONFIG_SEPARATE)    {
  143.         if (WriteTiFF(obp, row, 0) < 0)    goto    wrterr;
  144.         obp += cols;
  145.         if (WriteTiFF(obp, row, 1) < 0)    goto    wrterr;
  146.         obp += cols;
  147.         if (WriteTiFF(obp, row, 2) < 0)    goto    wrterr;
  148.         }
  149.         else    {
  150.         register byte*    bp = uimg.dest, *inr=obp, *ing=(obp+=cols);
  151.         if (revs) for (i=0, obp+=cols; i<cols; i++)    {
  152.             *bp++ = obp[i];
  153.             *bp++ = ing[i];
  154.             *bp++ = inr[i];
  155.         }
  156.         else    for (i=0, obp+=cols; i<cols; i++)    {
  157.             *bp++ = inr[i];    *bp++ = ing[i];    *bp++ = obp[i];
  158.         }
  159.         goto    wrt;
  160.         }    break;
  161.     case CFM_BITMAP:
  162.     if (uimg.in_color == ocfm)
  163.     case CFM_SGF:
  164.     case CFM_SCF:
  165.     case CFM_ILC:
  166.         uimg.dest = (char*)uimg.src + bytesperrow * row;
  167.     else    {
  168.     register int    col, bitshift;
  169.     register char    *bp = uimg.dest;
  170.  
  171.         bitshift = 8 - bitspersample;
  172.         *bp = 0;
  173.         for (col=0; col < cols; col++) {
  174.         if (obp[col])
  175.             *bp |= 1 << bitshift;
  176.         bitshift -= bitspersample;
  177.         if (bitshift < 0) {
  178.             bitshift = 8 - bitspersample;
  179.             *++bp = 0;
  180.         }
  181.         }
  182.     }
  183. wrt:    if (WriteTiFF(uimg.dest, row, 0) < 0)
  184. wrterr:        syserr("write scanline on row %d", row);
  185.     break;
  186.     default:    prgmerr(-1, "unknown format");
  187.     }
  188.     }
  189. TIFFFlushData(TIFFout);
  190. TIFFClose(TIFFout);
  191.  
  192. exit(0);
  193. }
  194.